package com.jvm.interpreter;

import com.jvm.interpreter.instruction.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: diy_simple_jvm
 * @description: 指令集合读取类
 * @author: CaoYong
 * @create: 2021-07-08 10:49
 **/
public class InstructionsReader {

    /**
     * 从文件读取指令集合
     * @param file
     * @return
     */
    public static Map<Integer, Instruction> parse(String file) {

        List<String> rawLines;

        try {
            rawLines = Files.readAllLines(Paths.get(file));
        } catch (Exception e) {
            System.out.println("file not found:" + file);
            return null;
        }

        if (rawLines.isEmpty()) {
            System.out.println("file is empty");
            return null;
        }

        List<String> line = rawLines.stream()
                .map(String::trim)
                .map(it -> it.replaceAll(":", " "))
                .map(it -> it.replaceAll(",", " "))
                .map(it -> it.replaceAll(" +", " "))
                .filter(it -> it.length() > 0)
                .collect(Collectors.toList());

        Map<Integer, Instruction> instructions = new HashMap<>();

        for (int i = 0; i < line.size(); i++) {
            String raw = line.get(i);
            String terms[] = raw.split(" ");
            int pc = Integer.parseInt(terms[0]);
            String inst = terms[1];

            Instruction instruction = null;

            switch (inst.toLowerCase()) {
                case "iconst_1":
                    instruction = new IConst1Inst();
                    break;
                case "istore_0":
                    instruction = new IStore0Inst();
                    break;
                case "iload_0":
                    instruction = new ILoad0Inst();
                    break;
                case "ireturn":
                    instruction = new IReturnInst();
                    break;
                case "iconst_0":
                    instruction = new IConst0Inst();
                    break;
                case "istore_1":
                    instruction = new IStore1Inst();
                    break;
                case "istore_2":
                    instruction = new IStore2Inst();
                    break;
                case "iload_1":
                    instruction = new ILoad1Inst();
                    break;
                case "iload_2":
                    instruction = new ILoad2Inst();
                    break;
                case "bipush":
                    instruction = new BiPushInst(Byte.parseByte(terms[2]));
                    break;
                case "if_icmpgt":
                    instruction = new IfIcmpGtInst(Integer.parseInt(terms[2]));
                    break;
                case "iadd":
                    instruction = new IAddInst();
                    break;
                case "iinc":
                    instruction = new IIncInst(Integer.parseInt(terms[2]), Integer.parseInt(terms[3]));
                    break;
                case "goto":
                    instruction = new GotoInst(Short.parseShort(terms[2]));
                    break;
            }

            if (null == instruction) {
                System.out.println("parse file failed, raw:" + raw);
                return null;
            }

            instructions.put(pc, instruction);
        }

        return instructions;
    }
}
